﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


class PrintTheRemainingSortedArray
{
    static void Main()
    {      
        List<int> arr = new List<int>{ 6, 1, 4, 3, 0, 3, 6, 4, 5 };
        List<int> arr2 = new List<int>();
        int copy = 0;
        int number = arr.Last();
        int element = 0;
        for (int i = 0; i < arr.Count - 1; i++)
        {
            copy = i;
            if (arr[i] > arr[i + 1])
            {
                arr2.Add(arr[i + 1]);
                arr.Remove(arr[i]);
                element = arr[i];
                while (i < arr.Count - 1)
                {
                    if (element > arr[i + 1] && i < arr.Count - 1)
                    {
                        arr.Remove(arr[i + 1]);                       
                    }
                    i++;
                }
                i = copy;
            }            
            else
            {
                arr2.Add(arr[i]);
                arr.Remove(arr[i + 1]);
                while (i < arr.Count - 1)
                {
                    if (arr[i] > arr[i + 1] && i < arr.Count - 1)
                    {
                        arr.Remove(arr[i + 1]);                      
                    }
                    i++;
                }
                i = copy;
            }
        }
        arr2.Add(number);
        foreach (int x in arr2)
        {
            Console.Write(x + ",");
        }
        Console.WriteLine();
    }
}